//
//  BIAppraiseViewController.m
//  biz
//
//  Created by 小龙 on 2017/3/22.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIAppraiseViewController.h"
#import "CWStarRateView.h"
@interface BIAppraiseViewController ()<CWStarRateViewDelegate>
@property (weak, nonatomic) IBOutlet UILabel *appraiseLabel;
@property (weak, nonatomic) IBOutlet CWStarRateView *starView;
/** score */
@property (nonatomic, assign) NSInteger score;

@end

@implementation BIAppraiseViewController


+ (instancetype)showWithURL:(NSString *)url uuid:(NSString *)uuid finishBlock:(void(^)())finishBlock
{
    UIView *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    BIAppraiseViewController *vc = [[self alloc] init];
    vc.view.frame = keyWindow.bounds;
    vc.finishBlock = finishBlock;
    vc.uuid = uuid;
    vc.view.backgroundColor = RGBA(0, 0, 0, 0.68);
    vc.appraiseUrl = url;
    
    [keyWindow addSubview:vc.view];
    return vc;
}


+ (instancetype)showWithURL:(NSString *)url uuid:(NSString *)uuid  type:(NSString *)type finishBlock:(void(^)())finishBlock
{
    UIView *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    BIAppraiseViewController *vc = [[self alloc] init];
    vc.view.frame = keyWindow.bounds;
    vc.finishBlock = finishBlock;
    vc.view.backgroundColor = RGBA(0, 0, 0, 0.68);
    vc.uuid = uuid;
    vc.type = type;
    vc.appraiseUrl = url;
    
    [keyWindow addSubview:vc.view];
    return vc;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.score = 5;
    self.starView.delegate = self;
}

- (IBAction)cancelBtnClick:(UIButton *)sender {
    [self finish];
}

- (IBAction)appraiseBtnClick:(UIButton *)sender {
    NSMutableDictionary *paramDic = [NSMutableDictionary dictionary];
    
    paramDic[@"uuid"] = self.uuid;
    paramDic[@"score"] = @(self.score);
    if (self.type.length) {
        paramDic[@"type"] = self.type;
    }
    
    [MBProgressHUD showAnimationHUD];
    [RequestTool POST:self.appraiseUrl parameters:paramDic progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        DeLog(@"%@",responseObject);
        
        NSInteger code = [responseObject[@"code"] integerValue];
        NSString *des = responseObject[@"des"];
    
        if (code == 1) {
            [MBProgressHUD showInfoMessage:des];
            [self finish];
        } else {
            [MBProgressHUD showErrorMessage:responseObject[@"des"]];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if ([AFNetworkReachabilityManager sharedManager].networkReachabilityStatus == AFNetworkReachabilityStatusNotReachable) {
            [MBProgressHUD showErrorMessage:@"网络不给力"];
        }
        else {
            [MBProgressHUD showErrorMessage:@"服务器繁忙"];
        }
        
    }];
}


- (void)finish
{
    [self.view removeFromSuperview];
    [self removeFromParentViewController];
    if (_finishBlock) {
        _finishBlock();
    }
}


- (void)starRateView:(CWStarRateView *)starRateView scroePercentDidChange:(CGFloat)newScorePercent
{
    int score = (int)(newScorePercent * 5);
    self.score = score;
    switch (score) {
        case 5:
            self.appraiseLabel.text = @"极好";
            break;
            
        case 4:
            self.appraiseLabel.text = @"较好";
            break;
            
            
        case 3:
            self.appraiseLabel.text = @"一般";
            break;
            
        case 2:
            self.appraiseLabel.text = @"较差";
            break;
        case 1:
            self.appraiseLabel.text = @"差";
            break;
            
            
        default:
            break;
    }

}


@end
